package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import com.glory.cqaospda.R
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.viewmodel.Inspect2Model
import com.glory.mvvmnet.base.BaseActivity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import kotlinx.android.synthetic.main.activity_inspect2.*
import kotlinx.android.synthetic.main.activity_inspect2.etLotId
import kotlinx.android.synthetic.main.activity_title.*
import java.lang.Exception

class Inspect2Activity: BaseActivity<Inspect2Model>(), View.OnClickListener {

    override fun initData() {

    }

    override fun initView(savedInstanceState: Bundle?) {
        tvBaseSubmit.visibility = View.GONE
        tvTitle.text = "制程检查"
        tvUserName.text = Constant.UserName
        linBuy.setOnClickListener(this)
        linInspect.setOnClickListener(this)
        linQA.setOnClickListener(this)
        linBinCheck.setOnClickListener(this)
        FreeApi_ActivityManager.getActivityManager().addActivity(this)

    }

    override fun layoutId(): Int {
        return R.layout.activity_inspect2
    }

    override fun onClick(view: View?) {
        if (Constant.isFastClick()) {
            when (view!!.id) {
                R.id.linBuy -> {
                    intent.putExtra("inspectType", "buyoff")
                }
                R.id.linInspect -> {
                    intent.putExtra("inspectType", "全检")
                }
                R.id.linQA -> {
                    intent.putExtra("inspectType", "QA")
                }
                R.id.linBinCheck -> {
                    intent.putExtra("inspectType", "bincheck")
                }
            }
            intent.putExtra("lotID", etLotId.text.toString())
            intent.setClass(this, InspectDetailsActivity::class.java)
            startActivity(intent)
        }
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        try {
            val view = window.decorView.findFocus() as EditText
            if (keyCode == 520) {
                when (view) {
                    etLotId -> {
                        etLotId.setText("")
                    }
                }
            }
        } catch (e: Exception){
            println(e.message)
        }
        return super.onKeyDown(keyCode, event)
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                108 -> {
                    finish()
                }
            }
        }
    }
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }
}